package cn.aias.websocket.adapter.http.socket.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * @Auther: cheng.tang
 * @Date: 2024/12/23
 * @Description: websocket-backend
 */
@Service
public class MyTextHandler extends TextWebSocketHandler {
    private static final Logger log = LoggerFactory.getLogger(MyTextHandler.class);

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        log.info(" afterConnectionEstablished 连接已建立 {} ", session.getId());
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        log.info(" handleTextMessage {} Payload {} PayloadLength {} ", session.getId(), message.getPayload(), message.getPayloadLength());
        session.sendMessage(new TextMessage("服务器收到消息大小:" + message.getPayloadLength() + ",内容:" + message.getPayload()));
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        log.info(" afterConnectionClosed  {} code {} reason {} ", session.getId(), status.getCode(), status.getReason());
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        log.info(" handleTransportError  {} {} ", session.getId(), exception);
    }


}
